package com.uwlaser.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import lombok.AllArgsConstructor;
import com.uwlaser.base.PageInfo;
import com.uwlaser.base.QueryHelpMybatisPlus;
import com.uwlaser.base.impl.CommonServiceImpl;
import com.uwlaser.utils.ConvertUtil;
import com.uwlaser.utils.PageUtil;
import com.uwlaser.domain.FactoryAttribute;
import com.uwlaser.service.FactoryAttributeService;
import com.uwlaser.service.dto.FactoryAttributeDto;
import com.uwlaser.service.dto.FactoryAttributeQueryParam;
import com.uwlaser.service.mapper.FactoryAttributeMapper;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Propagation;
import org.springframework.transaction.annotation.Transactional;
// 默认不使用缓存
//import org.springframework.cache.annotation.CacheConfig;
//import org.springframework.cache.annotation.CacheEvict;
//import org.springframework.cache.annotation.Cacheable;
import org.springframework.data.domain.Pageable;
import java.util.*;

/**
* @author QZK
* @date 2022-05-11
*/
@Service
@AllArgsConstructor
// @CacheConfig(cacheNames = FactoryAttributeService.CACHE_KEY)
@Transactional(propagation = Propagation.SUPPORTS, readOnly = true, rollbackFor = Exception.class)
public class FactoryAttributeServiceImpl extends CommonServiceImpl<FactoryAttributeMapper, FactoryAttribute> implements FactoryAttributeService {

    // private final RedisUtils redisUtils;
    private final FactoryAttributeMapper factoryAttributeMapper;

    @Override
    public PageInfo<FactoryAttributeDto> queryAll(FactoryAttributeQueryParam query, Pageable pageable) {
        IPage<FactoryAttribute> queryPage = PageUtil.toMybatisPage(pageable);
        IPage<FactoryAttribute> page = factoryAttributeMapper.selectPage(queryPage, QueryHelpMybatisPlus.getPredicate(query));
        return ConvertUtil.convertPage(page, FactoryAttributeDto.class);
    }

    @Override
    public List<FactoryAttributeDto> queryAll(FactoryAttributeQueryParam query){
        return ConvertUtil.convertList(factoryAttributeMapper.selectList(QueryHelpMybatisPlus.getPredicate(query)), FactoryAttributeDto.class);
    }

    @Override
    public FactoryAttribute getById(Integer id) {
        return factoryAttributeMapper.selectById(id);
    }

    @Override
    // @Cacheable(key = "'id:' + #p0")
    public FactoryAttributeDto findById(Integer id) {
        return ConvertUtil.convert(getById(id), FactoryAttributeDto.class);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int insert(FactoryAttributeDto resources) {
        FactoryAttribute entity = ConvertUtil.convert(resources, FactoryAttribute.class);
        FactoryAttribute factoryAttribute = new FactoryAttribute();
        factoryAttribute.setPid(entity.getPid());
        factoryAttribute.setAttrTag(entity.getAttrTag());
        Integer count = factoryAttributeMapper.selectCount(new QueryWrapper<>(factoryAttribute));
        if(count >= 1){
            throw new RuntimeException("属性编码【"+entity.getAttrTag()+"】不能重复！");
        }
        return factoryAttributeMapper.insert(entity);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int updateById(FactoryAttributeDto resources){
        FactoryAttribute entity = ConvertUtil.convert(resources, FactoryAttribute.class);
        int ret = factoryAttributeMapper.updateById(entity);
        FactoryAttribute factoryAttribute = new FactoryAttribute();
        factoryAttribute.setPid(entity.getPid());
        factoryAttribute.setAttrTag(entity.getAttrTag());
        Integer count = factoryAttributeMapper.selectCount(new QueryWrapper<>(factoryAttribute));
        if(count >= 2){
            throw new RuntimeException("属性编码【"+entity.getAttrTag()+"】不能重复！");
        }
        // delCaches(resources.id);
        return ret;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeByIds(Set<Integer> ids){
        // delCaches(ids);
        return factoryAttributeMapper.deleteBatchIds(ids);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public int removeById(Integer id){
        Set<Integer> set = new HashSet<>(1);
        set.add(id);
        return this.removeByIds(set);
    }

    /*
    private void delCaches(Integer id) {
        redisUtils.delByKey(CACHE_KEY + "::id:", id);
    }

    private void delCaches(Set<Integer> ids) {
        for (Integer id: ids) {
            delCaches(id);
        }
    }*/

    /*
    @Override
    public void download(List<FactoryAttributeDto> all, HttpServletResponse response) throws IOException {
      List<Map<String, Object>> list = new ArrayList<>();
      for (FactoryAttributeDto factoryAttribute : all) {
        Map<String,Object> map = new LinkedHashMap<>();
              map.put("属性标识", factoryAttribute.getAttrTag());
              map.put("属性名称", factoryAttribute.getAttrName());
              map.put("属性值", factoryAttribute.getAttrValue());
              map.put("创建人", factoryAttribute.getCreateBy());
              map.put("创建时间", factoryAttribute.getCreateTime());
              map.put("更新人", factoryAttribute.getUpdateBy());
              map.put("更新时间", factoryAttribute.getUpdateTime());
              map.put("资产ID", factoryAttribute.getPid());
              map.put("描述", factoryAttribute.getDescribes());
        list.add(map);
      }
      FileUtil.downloadExcel(list, response);
    }*/
}
